package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.BidDetailsAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.BidDetailsData;
import com.kkd.kkdapp.http.entity.BidDetailsItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

/****
 * 
 * @Date 2016-6-1
 * 
 * @author mty
 * 
 * @Name 投标详情
 * 
 */
public class BidDetailsActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private final static int CODE_ZERO = 1;
	private final static int CODE_ELSE = 2;
	private DecimalFormat df = new DecimalFormat("0.00");
	private RelativeLayout td_top_img;
	private TextView bid_name; // 标的名称
	private String bidTitle;
	private TextView bid_state; // 标的状态
	private String bidStatus;
	private String bidStatusCode;
	private TextView investment_amount; // 投资金额
	private String poiAmount;
	private TextView interest_rate; // 年化收益
	private String bidlilv;
	private TextView term; // 期限
	private String bidPeriodName;
	private TextView date; // 投标时间
	private String poiDate;
	private KkdXlistView mListview;
	private List<BidDetailsItem> billList;
	private BidDetailsAdapter adapter;
	private TextView agreement_bt; // 借款合同Button
	private String agreeloanUrl;
	private String appbiddetail;
	private LinearLayout top;
	private String bidId;
	private String docUrl;
	private String xyDescYw = "CRTA";
	private String contractNo;
	private String mPoiId;
	private ImageView id_no_content;
	private TextView id_no_content_text;
	private ListView listView;
	private String poiId;
	private boolean isTiyan;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZERO:
				bid_name.setText(bidTitle);
				if ("5002".equals(bidStatusCode)) {
					bid_state.setBackgroundResource(R.drawable.bidding_bg);
					bid_state.setText("招标中");
				} else if ("5003".equals(bidStatusCode)) {
					bid_state.setBackgroundResource(R.drawable.bidding_bg);
					bid_state.setText("已满标");
				} else if ("5004".equals(bidStatusCode)) {
					bid_state.setBackgroundResource(R.drawable.repaymentin_bg);
					bid_state.setText("还款中");
				} else if ("5005".equals(bidStatusCode)) {
					bid_state.setBackgroundResource(R.drawable.haspaidoff_bg);
					bid_state.setText("已还清");
				}
				investment_amount.setText(poiAmount);
				interest_rate.setText(bidlilv);
				term.setText(bidPeriodName);
				date.setText(poiDate);
				if (billList != null) {
					adapter = new BidDetailsAdapter(BidDetailsActivity.this,
							billList);
					mListview.setAdapter(adapter);
				}
				if (billList != null && billList.size() == 0) {
					id_no_content.setVisibility(View.VISIBLE);
					id_no_content_text.setVisibility(View.VISIBLE);
					id_no_content_text.setText("回款计划将在该标的放款后生成");
				} else if (billList.size() > 0) {
					id_no_content.setVisibility(View.GONE);
					id_no_content_text.setVisibility(View.GONE);
				}
				adapter.notifyDataSetChanged();
				mListview.stopRefresh();
				mListview.initState();
				break;
			case CODE_ELSE:
				if (billList == null) {
					id_no_content.setVisibility(View.VISIBLE);
					id_no_content_text.setVisibility(View.VISIBLE);
					id_no_content_text.setText("回款计划将在该标的放款后生成");
				} else {
					if (billList.size() == 0) {
						id_no_content.setVisibility(View.VISIBLE);
						id_no_content_text.setVisibility(View.VISIBLE);
						id_no_content_text.setText("回款计划将在该标的放款后生成");
					}

				}
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bid_details);
		initView();
		getMessage();
		setListener();
	}

	public void initView() {
		td_top_img = (RelativeLayout) this.findViewById(R.id.td_top_img);
		mListview = (KkdXlistView) this.findViewById(R.id.manage_lv);
		listView = mListview.mListView;
		listView.setDividerHeight(0);
		mListview.startRefresh();
		agreement_bt = (TextView) this.findViewById(R.id.agreement_bt);
		id_no_content = (ImageView) this.findViewById(R.id.id_no_content);
		id_no_content_text = (TextView) this
				.findViewById(R.id.id_no_content_text);
		View view = LayoutInflater.from(this).inflate(
				R.layout.biddetails_listview_head, null);
		bid_name = (TextView) view.findViewById(R.id.bid_name);
		bid_state = (TextView) view.findViewById(R.id.bid_state);
		investment_amount = (TextView) view
				.findViewById(R.id.investment_amount);
		interest_rate = (TextView) view.findViewById(R.id.interest_rate);
		term = (TextView) view.findViewById(R.id.term);
		date = (TextView) view.findViewById(R.id.date);
		top = (LinearLayout) view.findViewById(R.id.top);
		listView.addHeaderView(view);
	}

	public void setListener() {
		td_top_img.setOnClickListener(this);
		agreement_bt.setOnClickListener(this);
		mListview.setKKDScrollListener(this);
		top.setOnClickListener(this);
		if (isTiyan) {
			agreement_bt.setVisibility(View.GONE);
		} else {
			agreement_bt.setVisibility(View.VISIBLE);
		}
	}

	public void getMessage() {
		Intent intent = this.getIntent();
		poiId = intent.getStringExtra("poiId");
		isTiyan = intent.getBooleanExtra("isTiyan", false);
		requestData(poiId);
	}

	@Override
	public void onClick(View v) {

		Intent intent;

		switch (v.getId()) {
		case R.id.td_top_img:
			finish();
			break;
		case R.id.agreement_bt:
			if (!TextUtils.isEmpty(agreeloanUrl)) {
				intent = new Intent(this, KkdPdfWebActivity.class);
				intent.putExtra("url", agreeloanUrl);
				intent.putExtra("pdfUrl", docUrl);
				intent.putExtra("fileName", "【KKD】"+xyDescYw+"_"+contractNo+".pdf");
				startActivity(intent);
			}
			break;
		case R.id.top:
			LogUtil.e("TAG", "-----------------");
			if (!TextUtils.isEmpty(bidId) && !TextUtils.isEmpty(mPoiId)
					&& !TextUtils.isEmpty(appbiddetail)) {
				if (isTiyan) {
					intent = new Intent(this,
							ExperienceParticularsActivity.class);
				} else {
					intent = new Intent(this, RegularParticularsActivity.class);
				}
				intent.putExtra("bidId", bidId);
				intent.putExtra("poiId", mPoiId);
				intent.putExtra("appbiddetail", appbiddetail);
				startActivity(intent);
			}
			break;
		}

	}

	/**
	 * 请求接口
	 */
	public void requestData(String poiId) {

		LogUtil.e("poiId", poiId);

		KkdHttpMagager.getInstance().getBidDetailManager(poiId,
				BidDetailsData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						BidDetailsData data = (BidDetailsData) responseBody;
						bidTitle = data.getBidTitle();
						bidStatus = data.getBidStatus();
						bidStatusCode = data.getBidStatusCode();
						poiAmount = data.getPoiAmount();
						bidlilv = data.getBidlilv();
						bidPeriodName = data.getBidPeriodName();
						poiDate = data.getPoiDate();
						billList = data.getBillList();
						agreeloanUrl = data.getAgreeloanUrl();
						appbiddetail = data.getAppbiddetail();
						docUrl = data.getDocUrl();
						xyDescYw = data.getXyDescYw();
						contractNo = data.getContractNo();
						bidId = data.getBidId();
						mPoiId = data.getPoiId();
						mHandler.sendEmptyMessage(CODE_ZERO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						mHandler.sendEmptyMessage(CODE_ELSE);
					}
				});
	}

	@Override
	public void onRefresh() {
		requestData(poiId);
	}

	@Override
	public void onLoadMore() {

	}
}
